<?php


namespace App\Http\Controllers\Platform;


use App\Http\Requests\Platform\AuthRuleRequest;
use App\Services\AuthRuleService;
use App\Utils\ResultUtil;

class AuthRuleController
{

    /**
     * 权限节点树形列表
     * @return array
     */
    public function index(AuthRuleService $rule)
    {
        return ResultUtil::data($rule->getTreeList());
    }


    /**
     * 新增权限节点
     * @param AuthRuleRequest $authRuleRequest
     * @param AuthRuleService $authRuleService
     * @return array
     * @throws \App\Exceptions\ApiException
     * @throws \Illuminate\Validation\ValidationException
     */
    public function add(AuthRuleRequest $authRuleRequest, AuthRuleService $authRuleService)
    {
        $authRuleRequest->scene('add')->validate();
        $data = $authRuleRequest->validationData();
        $authRuleService->add($data);
        return ResultUtil::success();
    }


    /**
     * 修改权限节点
     * @param AuthRuleRequest $authRuleRequest
     * @param AuthRuleService $authRuleService
     * @return array
     * @throws \App\Exceptions\ApiException
     * @throws \Illuminate\Validation\ValidationException
     */
    public function edit(AuthRuleRequest $authRuleRequest,AuthRuleService $authRuleService){
        $authRuleRequest->scene('edit')->validate();
        $data=$authRuleRequest->validationData();
        $authRuleService->edit($data);
        return ResultUtil::success();
    }


    /**
     * 删除权限
     * @param int $id
     * @param AuthRuleService $authRuleService
     * @return array
     * @throws \App\Exceptions\ApiException
     */
    public function del(int $id,AuthRuleService $authRuleService){
        $authRuleService->del($id);
        return ResultUtil::success();
    }

}
